// pages/doctor-authentication-2/doctor-authentication-2.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageone: "",
    imagetwo: "",
    imagethree: "",
    cardFront:"",
    cardOpposite:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(e) {
    console.log('options', e)
    wx.setStorageSync("loginjudge", 666);
    this.setData({
      url: app.globalData.imgUrl,
      name: e.name,
      avatarUrls: e.avatarUrls,
      hospital: e.hospital,
      provinceid: e.provinceid,
      cityid: e.cityid,
      districtid: e.districtid,
      oneid: e.oneid,
      twoid: e.twoid,
      threeid: e.threeid,
      lng: e.lng,
      lat: e.lat,
      address: e.address,
      content: e.content,
      service: e.service,                                          
    })
  },
  //保存
  addsave: function() {
    var that = this;
    var name = that.data.name;
    var avatarUrls = that.data.avatarUrls;
    var hospital = that.data.hospital;
    var provinceid = that.data.provinceid;
    var cityid = that.data.cityid;
    var districtid = that.data.districtid;
    var oneid = that.data.oneid;
    var twoid = that.data.twoid;
    var threeid = that.data.threeid;
    var content = that.data.content;
    var imageone = that.data.imageone;
    var imagetwo = that.data.imagetwo;
    var imagethree = that.data.imagethree;
    var lng = that.data.lng;
    var lat = that.data.lat;
    var address = that.data.address;
    var service = that.data.service;      
    var cardFront = that.data.cardFront;      
    var cardOpposite = that.data.cardOpposite;          
    if (cardFront == '') {
      wx.showToast({
        title: '请上传身份证正面照片',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (cardOpposite == '') {
      wx.showToast({
        title: '请上传身份证背面照片',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (imageone == '') {
      wx.showToast({
        title: '请上传医生职业证书复印件',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (imagetwo == '') {
      wx.showToast({
        title: '请上传医师资格证',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (imagethree == '') {
      wx.showToast({
        title: '请上传近期体检表',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    // if (imageone == '' && imagetwo == '' && imagethree == '') {
    //   wx.showToast({
    //     title: '请您至少上传一张证件图片',
    //     icon: 'none',
    //     duration: 1000
    //   })
    //   return false;
    // }
    console.log('name', name);
    console.log('photo', avatarUrls);
    console.log('company', hospital);
    console.log('service', service);
    console.log('proId', provinceid);
    console.log('areaId', cityid);
    console.log('townId', districtid);
    console.log('intro', content);
    console.log('seniority', imageone);
    console.log('profession', imagetwo);
    console.log('beauty', imagethree);
    console.log('lat', lat);
    console.log('lng', lng);
    console.log('cardFront', cardFront);
    console.log('cardOpposite', cardOpposite);    
    console.log('address', address);    
    wx.request({
      url: app.globalData.baseUrl + 'doctor/doctor/finish',
      data: {
        photo: avatarUrls,
        name: name,
        company: hospital,
        service: service,
        proId: provinceid,
        areaId: cityid,
        townId: districtid,
        intro: content,
        seniority: imageone,
        profession: imagetwo,
        beauty: imagethree,
        lat: lat,
        lng: lng,
        cardFront:cardFront,
        cardOpposite: cardOpposite,      
        address: address
      },
      header: {
        'sign': app.globalData.sign,
        'token': app.globalData.token,
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
      method: 'POST',
      success(res) {
        console.log('完善医生端个人信息:', res)
        if (res.data.status_code == 200) {
          wx.navigateTo({
            url: '../doctor-authentication-3/doctor-authentication-3',
          })
        }
      }
    })
  },
  // 身份证正面上传图片
  cardFront: function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success: function (e) {
        console.log(e);
        var a = e.tempFilePaths[0];
        wx.uploadFile({
          url: app.globalData.baseUrl + 'doctor/doctor/uploadCertFile',
          filePath: a,
          name: "file",
          header: {
            'sign': app.globalData.sign,
            'token': app.globalData.token,
            "Content-Type": "multipart/form-data"
          },
          formData: {},
          success: function (res) {
            console.log('资格证上传结果:', res);
            var path = JSON.parse(res.data).data[0].filePath;
            that.setData({
              cardFront: path
            })
          },
          fail: function (e) {
            console.log(e)
          }
        })
      }
    })
  },
  // 身份证反面上传图片
  cardOpposite: function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success: function (e) {
        console.log(e);
        var a = e.tempFilePaths[0];
        wx.uploadFile({
          url: app.globalData.baseUrl + 'doctor/doctor/uploadCertFile',
          filePath: a,
          name: "file",
          header: {
            'sign': app.globalData.sign,
            'token': app.globalData.token,
            "Content-Type": "multipart/form-data"
          },
          formData: {},
          success: function (res) {
            console.log('资格证上传结果:', res);
            var path = JSON.parse(res.data).data[0].filePath;
            that.setData({
              cardOpposite: path
            })
          },
          fail: function (e) {
            console.log(e)
          }
        })
      }
    })
  },
  // 资格证上传图片
  updateone: function() {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success: function(e) {
        console.log(e);
        var a = e.tempFilePaths[0];
        wx.uploadFile({
          url: app.globalData.baseUrl + 'doctor/doctor/uploadCertFile',
          filePath: a,
          name: "file",
          header: {
            'sign': app.globalData.sign,
            'token': app.globalData.token,
            "Content-Type": "multipart/form-data"
          },
          formData: {},
          success: function(res) {
            console.log('资格证上传结果:', res);
            var path = JSON.parse(res.data).data[0].filePath;
            that.setData({
              imageone: path
            })
          },
          fail: function(e) {
            console.log(e)
          }
        })
      }
    })
  },
  // 职业证上传图片
  updatetwo: function() {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success: function(e) {
        console.log(e);
        var a = e.tempFilePaths[0];
        wx.uploadFile({
          url: app.globalData.baseUrl + 'doctor/doctor/uploadCertFile',
          filePath: a,
          name: "file",
          header: {
            'sign': app.globalData.sign,
            'token': app.globalData.token,
            "Content-Type": "multipart/form-data"
          },
          formData: {},
          success: function(res) {
            console.log('职业证上传结果:', res);
            var path = JSON.parse(res.data).data[0].filePath;
            that.setData({
              imagetwo: path
            })
          },
          fail: function(e) {
            console.log(e)
          }
        })
      }
    })
  },
  // 医疗美容医生主诊医生资格证上传图片
  updatethree: function() {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success: function(e) {
        console.log(e);
        var a = e.tempFilePaths[0];
        wx.uploadFile({
          url: app.globalData.baseUrl + 'doctor/doctor/uploadCertFile',
          filePath: a,
          name: "file",
          header: {
            'sign': app.globalData.sign,
            'token': app.globalData.token,
            "Content-Type": "multipart/form-data"
          },
          formData: {},
          success: function(res) {
            console.log('医疗美容医生主诊医生资格证上传结果:', res);
            var path = JSON.parse(res.data).data[0].filePath;
            that.setData({
              imagethree: path
            })
          },
          fail: function(e) {
            console.log(e)
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})